package com.hao.hospital.controller;


import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.DocumentException;

import com.alibaba.fastjson.JSON;
import com.hao.hospital.core.param.GlobalMap;
import com.hao.hospital.service.UserService;
import com.hao.hospital.service.WeiXinService;
import com.hao.hospital.util.MessageUtil;
import com.hao.hospital.util.WeiXinUtil;
import com.hao.hospital.weixin.bean.TextMessage;
import com.jfinal.core.Controller;

public class WeiXinAction extends Controller {
	/**
	 * 微信服务器接收信息地址
	 * @throws DocumentException 
	 * @throws IOException 
	 */
	public void get() {
		HttpServletRequest request=getRequest();
		try {
			if (request != null) {
				Map<String, String> map = MessageUtil.xmlToMap(request);
				System.err.println(map);
				String toUserName = map.get("ToUserName");
				String fromUserName = map.get("FromUserName");
				String createTime = map.get("CreateTime");
				String msgType = map.get("MsgType");
				String event = map.get("Event");
				if(msgType.equals("event")&& event.equals("subscribe")){
					TextMessage text=new TextMessage();
					text.setFromUserName(toUserName);
					text.setToUserName(fromUserName);
					text.setMsgType("text");
					text.setCreateTime(new Date().getTime());
					text.setContent("欢迎来到骨科门诊自助服务系统,\n预约点击左边按钮,\n查询点击右边按钮,\n新用户可点击任意按钮进行信息完善。");
					String message= MessageUtil.textMessageToXml(text);
					renderText(message);
					return;
				}
			} 
		} catch (Exception e) {
			System.out.println("");
		}
		String echostr = getPara("echostr");
		renderText(echostr);
	}

	/**
	 * 创建公众号按钮
	 */
	public void createMenu() {
		String menu = JSON.toJSONString(WeiXinUtil.me().initMenu());
		int result = WeiXinService.me().createMenu(WeiXinService.me().getToken(), menu);
		if (result == 0) {
			renderText("创建菜单成功");
			return;
		} else {
			renderText("创建失败");
			return;
		}
	}
	/**
	 * 回调地址
	 */
	public void callback() {
		System.out.println("跳转成功");
		String code = getPara("code");// 5分钟
		Map data = WeiXinService.me().getUserToken(code);
		System.out.println(data);
		if (data.get("errcode") == null) {
			String access_token=data.get("access_token").toString();
			String refresh_token=data.get("refresh_token").toString();
			String openid=data.get("openid").toString();
			GlobalMap.configMap.put("user_access_token", access_token);
			GlobalMap.configMap.put("user_refresh_token",refresh_token);
			setSessionAttr("openid", openid);
			int flag=WeiXinService.me().getUserInfo(access_token, openid);
			System.out.println("标识的值："+flag);
			WeiXinService.me().afterAuth(flag, this);
		} else {
				renderText(data.get("errcode").toString());
		}
	}
	public void load(){
		redirect(getSessionAttr("url").toString());
	}
	
	
	/**
	 * 添加用户信息学
	 */
	public void addUserInfo(){
		String name=getPara("name");
		String address=getPara("address");
		String newpass=getPara("newpass");
		String renewpass=getPara("renewpass");
		String openid=getSessionAttr("openid");	
		System.out.println("打印参数："+name+address+newpass+renewpass+openid);
		if(newpass.equals(renewpass)){
			if(UserService.me().addUserInfo(openid, address, name, newpass)==1){
				redirect("../main/updateinfo");
				return;
			}
		}else{
			redirect("../main/updateinfo");
			return;
			//WeiXinService.me().afterAuth(0, this);
		}
	}
}
